package com.cw.jeeyt.service.impl.security;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityRoleResourceBo;
import com.cw.jeeyt.dao.intf.security.SecurityRoleResourceDao;
import com.cw.jeeyt.dao.model.security.SecurityRoleResourceDo;
import com.cw.jeeyt.service.converter.security.SecurityRoleResourceConverter;
import com.cw.jeeyt.service.intf.security.SecurityRoleResourceService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 角色资源业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SecurityRoleResourceServiceImpl implements SecurityRoleResourceService {

    private static final Logger logger = LoggerFactory.getLogger(SecurityRoleResourceServiceImpl.class);
    @Autowired
    private SecurityRoleResourceDao securityRoleResourceDao;

    /**
     * 分页查询角色资源
     *
     * @param roleResourceBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SecurityRoleResourceBo> queryByPage(SecurityRoleResourceBo roleResourceBo, Integer startNum,
        Integer pageSize) throws Exception {

        if (roleResourceBo == null) {
            return null;
        }
        PageInfo<SecurityRoleResourceDo> pageInfo = securityRoleResourceDao.queryByPage(
            SecurityRoleResourceConverter.bo2Do(roleResourceBo), startNum, pageSize);
        return new PageInfo<>(pageInfo, SecurityRoleResourceConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据角色id查询角色资源信息
     */
    @Override
    public List<SecurityRoleResourceBo> queryRoleResourceByRoleId(Long roleId) throws Exception {
        if (null == roleId) {
            logger.error("角色Id为空");
            return null;
        }
        return SecurityRoleResourceConverter.doList2BoList(securityRoleResourceDao.queryRoleResourceByRoleId(roleId));
    }

    /**
     * 根据主键查寻角色资源信息
     *
     * @param id
     * @return
     */
    @Override
    public SecurityRoleResourceBo getRoleResourceById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }

        return SecurityRoleResourceConverter.do2Bo(securityRoleResourceDao.getById(id));
    }

    @Override
    public SecurityRoleResourceBo getByRoleIdAndResId(Long roleId, Long resId) throws Exception {
        if (null == roleId) {
            logger.error("--角色id为空--");
            return null;
        }

        return SecurityRoleResourceConverter.do2Bo(securityRoleResourceDao.getByRoleIdAndResId(roleId, resId));
    }

    /**
     * 新增角色资源
     *
     * @param roleResourceBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SecurityRoleResourceBo roleResourceBo) throws Exception {

        if (roleResourceBo == null) {
            logger.error("角色资源为空");
            return null;
        }
        SecurityRoleResourceDo roleResource = SecurityRoleResourceConverter.bo2Do(roleResourceBo);
        roleResource.setId(SequenceTool.nextId());
        return securityRoleResourceDao.insert(roleResource);
    }

    /**
     * 更新角色资源信息
     *
     * @param roleResourceBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SecurityRoleResourceBo roleResourceBo) throws Exception {

        if (roleResourceBo == null) {
            logger.error("角色资源为空");
            return;
        }

        securityRoleResourceDao.update(SecurityRoleResourceConverter.bo2Do(roleResourceBo));
    }

    /**
     * 删除角色资源
     *
     * @param roleResourceBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SecurityRoleResourceBo roleResourceBo) throws Exception {

        if (roleResourceBo == null) {
            logger.error("角色资源为空");
            return;
        }
        securityRoleResourceDao.delete(SecurityRoleResourceConverter.bo2Do(roleResourceBo));
    }

    @Override
    public void insertRoleResource(Long roleId, List<Long> roleResources) throws Exception {
        if (roleId == null || (roleResources == null || roleResources.isEmpty())) {
            return;
        }
        for (Long resId : roleResources) {
            SecurityRoleResourceDo roleResource = new SecurityRoleResourceDo();
            String id = SequenceTool.nextId();
            roleResource.setId(id);
            roleResource.setRoleId(roleId);
            roleResource.setResId(resId);
            securityRoleResourceDao.insert(roleResource);
        }
    }
}
